package my.board;

import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;

import my.climbing.*;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.AdapterView.OnItemClickListener;

public class ImageList extends Activity {
	
	private Context mContext;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.image_list);
        mContext = this;
        
        GridView gv = (GridView)findViewById(R.id.ImgGridView);
        final ImageAdapter ia = new ImageAdapter(this);
        gv.setAdapter(ia);
        gv.setOnItemClickListener(new OnItemClickListener(){
        	public void onItemClick(AdapterView<?> parent, View v, int position, long id){
        		ia.callImageViewer(position);
        	}
        });
	}
	
	/**========================================== 
	 * 		        Adapter class 
	 * ==========================================*/
	public class ImageAdapter extends BaseAdapter {
		private String imgData;
		private String geoData;
		private ArrayList<String> thumbsDataList;
		private ArrayList<String> thumbsIDList;
		
		ImageAdapter(Context c){
			mContext = c;
			thumbsDataList = new ArrayList<String>();
			thumbsIDList = new ArrayList<String>();
			getThumbInfo(thumbsIDList, thumbsDataList);
		}
		
		public final void callImageViewer(int selectedIndex){
			String imgPath = getImageInfo(imgData, geoData, thumbsIDList.get(selectedIndex));
			SanWrite.et4.setText(imgPath);
			finish();
		}
		
		public boolean deleteSelected(int sIndex){
			return true;
		}
		
		public int getCount() {
			return thumbsIDList.size();
		}

		public Object getItem(int position) {
			return position;
		}

		public long getItemId(int position) {	
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			ImageView imageView;
			if (convertView == null){
				imageView = new ImageView(mContext);
				imageView.setLayoutParams(new GridView.LayoutParams(95, 95));
				imageView.setAdjustViewBounds(false);
				imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
				imageView.setPadding(2, 2, 2, 2);
			}else{
				imageView = (ImageView) convertView;
			}
			BitmapFactory.Options bo = new BitmapFactory.Options();
			bo.inSampleSize = 8;
			Bitmap bmp = BitmapFactory.decodeFile(thumbsDataList.get(position), bo);
			Bitmap resized = Bitmap.createScaledBitmap(bmp, 95, 95, true);
			imageView.setImageBitmap(resized);
			return imageView;
		}
		
		private void getThumbInfo(ArrayList<String> thumbsIDs, ArrayList<String> thumbsDatas){
			String[] proj = {MediaStore.Images.Media._ID,
							 MediaStore.Images.Media.DATA,
							 MediaStore.Images.Media.DISPLAY_NAME,
							 MediaStore.Images.Media.SIZE};
			
			Cursor imageCursor = managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
					proj, null, null, null);
			
			if (imageCursor != null && imageCursor.moveToFirst()){
				String title;
				String thumbsID;
				String thumbsImageID;
				String thumbsData;
				String data;
				String imgSize;
				
				int thumbsIDCol = imageCursor.getColumnIndex(MediaStore.Images.Media._ID);
				int thumbsDataCol = imageCursor.getColumnIndex(MediaStore.Images.Media.DATA);
				int thumbsImageIDCol = imageCursor.getColumnIndex(MediaStore.Images.Media.DISPLAY_NAME);
				int thumbsSizeCol = imageCursor.getColumnIndex(MediaStore.Images.Media.SIZE);
				int num = 0;
				do {
					thumbsID = imageCursor.getString(thumbsIDCol);
					thumbsData = imageCursor.getString(thumbsDataCol);
					thumbsImageID = imageCursor.getString(thumbsImageIDCol);
					imgSize = imageCursor.getString(thumbsSizeCol);
					num++;
					if (thumbsImageID != null){
						thumbsIDs.add(thumbsID);
						thumbsDatas.add(thumbsData);
					}
				}while (imageCursor.moveToNext());
			}
			imageCursor.close();
			return;
		}
		
		private String getImageInfo(String ImageData, String Location, String thumbID){
			String imageDataPath = null;
			String[] proj = {MediaStore.Images.Media._ID,
					 MediaStore.Images.Media.DATA,
					 MediaStore.Images.Media.DISPLAY_NAME,
					 MediaStore.Images.Media.SIZE};
			Cursor imageCursor = managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
					proj, "_ID='"+ thumbID +"'", null, null);
			
			if (imageCursor != null && imageCursor.moveToFirst()){
				if (imageCursor.getCount() > 0){
					int imgData = imageCursor.getColumnIndex(MediaStore.Images.Media.DATA);
					imageDataPath = imageCursor.getString(imgData);
				}
			}
			imageCursor.close();
			return imageDataPath;
		}
	}
}
